package com.zelin.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.apache.ibatis.reflection.MetaObject;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;

/**
 * ------------------------------
 * 功能：mybatis配置
 * 作者：WF
 * 微信：hbxfwf13590332912
 * 创建时间：2021/10/19-15:56
 * ------------------------------
 */
@Configuration                      //相当于: applicationContext.xml文件
@MapperScan("com.zelin.mapper")
public class MyBatisPlusConfig implements MetaObjectHandler {

    /**
     * 新的分页插件,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false
     * 避免缓存出现问题(该属性会在旧插件移除后一同移除)
     */
    @Bean               //相当于<bean>标签
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }

    @Override
    public void insertFill(MetaObject metaObject) {
        // 或者
        this.fillStrategy(metaObject, "createTime", LocalDate.now().toString()); // 也可以使用(3.3.0 该方法有bug)
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        // 或者
        this.fillStrategy(metaObject, "updateTime", LocalDate.now().toString()); // 也可以使用(3.3.0 该方法有bug)

    }
}
